package com.wwu.system.common.utils;

import com.wwu.system.entity.SysMenu;
import com.wwu.system.vo.MenuVO;
import com.wwu.system.vo.TreeVO;
import com.wwu.system.vo.XmSelectVO;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 一蓑烟雨
 * @version 1.0.0
 * @date 2023-06-30 16:42
 */
public class TreeUtils {

    /**
     * 递归查找指定节点的子节点，返回XmSelectVO集合
     * @param menuId    当前菜单id
     * @param menuId    需要选中的节点
     * @param menuType  父菜单级别(0:模块;1:目录;2:菜单;3:按钮)
     * @param menuList  待循环的菜单集合
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/6/27 15:56
     */
    public static List<XmSelectVO> getChildrenSelect(String menuId, String selectMenuId, Integer menuType, List<SysMenu> menuList){
        boolean stopFlag = false;
        List<XmSelectVO> childMenuList = new ArrayList<>();
        for (SysMenu sysMenu: menuList) {
            if (sysMenu.getParentMenuId().equals(menuId)) {
                XmSelectVO xmSelect = new XmSelectVO();
                xmSelect.setValue(sysMenu.getMenuId());
                xmSelect.setName(sysMenu.getMenuName());
                xmSelect.setSort(sysMenu.getMenuSort());
                xmSelect.setDisabled(0 == sysMenu.getMenuStatus() ? false: true);
                xmSelect.setParentId(sysMenu.getParentMenuId());
                if(sysMenu.getMenuId().equals(selectMenuId)){
                    xmSelect.setSelected(true);
                }
                childMenuList.add(xmSelect);

                if(sysMenu.getMenuType().equals(menuType)){
                    stopFlag = true;
                }
            }
        }

        if(!stopFlag){
            for (XmSelectVO xmSelect: childMenuList) {
                xmSelect.setChildren(getChildrenSelect(xmSelect.getValue(),selectMenuId,menuType,menuList));
            }
        }

        return childMenuList;
    }


    /**
     * 递归查找指定节点的子节点，返回MenuVO集合
     * @param id  父菜单id
     * @param menuList  待循环的菜单集合
     * @return java.util.List<com.wwu.system.vo.MenuVO>
     * @author 一蓑烟雨
     * @date 2023/6/30 17:30
     */
    public static List<MenuVO> getChildMenu(String id, List<MenuVO> menuList){
        List<MenuVO> childMenuList = new ArrayList<>();
        for (MenuVO menu: menuList) {
            if (menu.getPid().equals(id)) {
                childMenuList.add(menu);
            }
        }

        for (MenuVO menu: childMenuList) {
            menu.setChild(getChildMenu(menu.getId(),menuList));
        }

        return childMenuList;
    }

    /**
     * 递归查找指定节点的子节点，返回TreeVo集合
     * @param id  父菜单id
     * @param menuList  待循环的菜单集合
     * @return java.util.List<TreeVo>
     * @author 一蓑烟雨
     * @date 2023/8/16 20:22
     */
    public static List<TreeVO> getChildTree(String id, List<TreeVO> menuList){
        List<TreeVO> childMenuList = new ArrayList<>();
        for (TreeVO treeVO: menuList) {
            if (treeVO.getPid().equals(id)) {
                childMenuList.add(treeVO);
            }
        }

        for (TreeVO treeVO: childMenuList) {
            treeVO.setChildren(getChildTree(treeVO.getId(),menuList));
        }

        return childMenuList;
    }
}
